import re, os, atexit
def runUnitTests(testerPath):
    print '='*30, 'Running unit tests', '='*30
    os.system(testerPath)

Import('EnvHelper')

envHelper = EnvHelper()
e = envHelper.env

envHelper.useLibrary('transupport')

testDirPath     = EnvHelper.OutputDirPath + 'test/transupport/'

################## SET UP REAL WORK ##################
# Compile test executables
sourceFiles = Glob('*.cpp', strings=True)

for file in sourceFiles:
    execName = re.sub(r'\.cpp$','',file)
    if (execName != "tTemplate"):
        prog = e.Program(testDirPath + execName, file)
        Alias('test',prog)

# Copy auxiliary files
#auxFiles = Glob('*.in', strings=True)
#e.Install(testSupportPath, auxFiles)

# Copy the python script
runTests = e.Install(EnvHelper.OutputDirPath + 'test/', 'runTests.py')
Alias('test',runTests)

# Run the unit tests if we're using 'test' mode
if 'test' in map(str, BUILD_TARGETS):
    atexit.register(runUnitTests, str(runTests[0]))

# copy the python script to the install directory maybe
envHelper.installBinary('runTests.py')
